﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Net;

namespace Chaos.commands
{
    public class disconnect : ICommand
    {
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public virtual bool CanExecute(object parameter)
        {
            network.gameclient c = System.Windows.Application.Current.FindResource("game_client") as network.gameclient;
            if (c != null)
            {
                if (c.Running)
                {
                    return true;
                }
            }
            network.gameserver s = System.Windows.Application.Current.FindResource("game_client") as network.gameserver;
            if (s != null)
            {
                if (s.Running)
                {
                    return true;
                }
            }
            
            return false;
        }

        public virtual void Execute(object parameter)
        {
            network.gameclient c = System.Windows.Application.Current.FindResource("game_client") as network.gameclient;
            if (c != null)
            {
                c.shutdown();
            }

            network.gameserver s = System.Windows.Application.Current.FindResource("game_server") as network.gameserver;
            if (s != null)
            {
                s.shutdown();
            }

            network.detectionserver ds = System.Windows.Application.Current.FindResource("detection_server") as network.detectionserver;
            if (ds != null)
            {
                ds.shutdown();
            }

            network.detectionclient dc = System.Windows.Application.Current.FindResource("detection_client") as network.detectionclient;
            if (dc != null)
            {
                dc.shutdown();
            }

            view.manager m = System.Windows.Application.Current.FindResource("view_manager") as view.manager;
            if (m != null)
            {
                MainWindow w = m.Controls["main_window"] as MainWindow;
                if (w != null)
                {
                    w.ScreenGrid_show(w.ConnectScreen);
                }
            }
        }
    }
}
